package com.kushe.dao;

import com.kushe.entity.UserAndGroup;
import com.kushe.util.core.DbUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.springframework.data.redis.listener.Topic;

import java.util.List;

/**
 * Created by WSDevotion on 2015/5/2.
 */
public class UserAndGroupDao {
    public static UserAndGroup get(int id) {
        return DbUtils.getNutzDao().fetch(UserAndGroup.class, id);
    }


//    public static UserAndGroup addUserAndGroup(Comment comment) {
//        Dao dao = DbUtils.getDao();
//        Topics topics = dao.fetch(Topics.class, comment.getTopic_id());
//        if (topics != null) {
//            dao.insert(comment);
//            return comment;
//        } else
//            return null;
//    }

    public static List<UserAndGroup> getUserAndGroupByuser_id(int user_id){
        Dao dao = DbUtils.getNutzDao();
        List<UserAndGroup> list = dao.query(UserAndGroup.class, Cnd.where("user_id", "=", user_id));
        return list;
    }

    public static List<UserAndGroup> getMesByuser_idAndGroup_id(int user_id, int group_id){
        Dao dao = DbUtils.getNutzDao();
        Criteria cri = Cnd.cri();
        cri.where().andEquals("user_id", user_id);
        cri.where().andEquals("group_id", group_id);
        List<UserAndGroup> list = dao.query(UserAndGroup.class, cri);
        return list;
    }

    public static UserAndGroup addUserAndGroup(UserAndGroup userAndGroup){
        Dao dao = DbUtils.getNutzDao();
        Criteria cri = Cnd.cri();
        cri.where().andEquals("user_id", userAndGroup.getUser_id());
        cri.where().andEquals("group_id", userAndGroup.getGroup_id());
        List<UserAndGroup> list = dao.query(UserAndGroup.class, cri);
        if(list.size()==0) {
            dao.insert(userAndGroup);
            return userAndGroup;
        }
        return null;
    }

//    public static QueryResult getByTopics(int pageNumber, int topic_id) {
//        Dao dao = DbUtils.getDao();
//        Pager pager = dao.createPager(pageNumber, Config.PAGE_SIZE);
//        List<Group> list = null;
//        Topics topics = dao.fetch(Topics.class, topic_id);
//        if(topics!=null) {
//            list = dao.query(Group.class, Cnd.where("topic_id", "=", topic_id).orderBy().asc("zan"), pager);
//        }else{
//            return null;
//        }
//        pager.setRecordCount(dao.count(Topic.class));
//        return new QueryResult(list, pager);
//    }



    public static void updateGroup(Topic topic){
        Dao dao = DbUtils.getNutzDao();
        dao.update(topic);
    }
}
